//
//  WYReceivedController.m
//  Boss
//
//  Created by admin on 2018/6/7.
//  Copyright © 2018年 admin. All rights reserved.
//

#import "WYReceivedController.h"
#import "WYRedSectionView.h"
#import "WYRedPacketListCell.h"
#import "WYMYReceiveList.h"
#import "WYMYReceive.h"
#import "WYSelectYearView.h"
#import "WYRedPacketMessageController.h"
#import "WYNearRedPacket.h"
#import "WYExclusiveController.h"
@interface WYReceivedController ()<UITableViewDelegate,UITableViewDataSource,WYRedSectionViewDelegate>
{
    BOOL refreshState;
}
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *dataArray;
@property (nonatomic,strong)WYMYReceive *receive;
@property (nonatomic,strong)WYRedSectionView *sectionView;
@property (nonatomic,assign)BOOL refresh;
@property (nonatomic,strong) NSString *year;
@property (nonatomic,assign)NSUInteger page;
@end

@implementation WYReceivedController
-(NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc]init];
    }
    return _dataArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.refresh = NO;
    self.page = 1;
    // Do any additional setup after loading the view.
    [self setupUI];
    [self loadDataWithYear:@"2018"];
    self.year = @"2018";
    WEAKSELF
    self.tableView.mj_footer=[MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        weakSelf.refresh=NO;
        weakSelf.page++;
       // [weakSelf loadTableData];
        [weakSelf loadDataWithYear:self.year];
    }];
    
    
    
    
    
    
    
}
-(void)loadDataWithYear:(NSString *)year
{
    WEAKSELF
    [WYHttpServer getRedPacketMyReceiveWithYear:year SucessBlock:^(BOOL success, id response, NSString *errorMessage) {
        
        if (success) {
            weakSelf.receive = [WYMYReceive getMyReceiveModelFromJson:response[@"result"]];
            [weakSelf.tableView reloadData];
        }else{
            [weakSelf toast:errorMessage];
        }
    }];
    NSString *page = [NSString stringWithFormat:@"%ld",self.page];
    [WYHttpServer getRedPacketMyReceiveRecordWithYear:year WithCurrentPage:page SucessBlock:^(BOOL success, id response, NSString *errorMessage) {
        if (success) {
            if (weakSelf.refresh) {
                [weakSelf.dataArray removeAllObjects];
            }
            
            [weakSelf.dataArray addObjectsFromArray:[WYMYReceiveList getMYReceiveListFromJson:response[@"result"][@"detail"]]];
            [weakSelf.tableView reloadData];
            if ([response[@"result"][@"hasNextPage"] integerValue]) {
                [weakSelf.tableView.mj_footer endRefreshing];
                
                
                
            }else{
                [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
            }
            
            
            
        }else{
            [weakSelf toast:errorMessage];
        }
        
        
        
    }];
    
    
    
}
-(void)setupUI
{
    [self.view addSubview:self.tableView];
    self.tableView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight-88-44+20);
    [self.tableView registerNib:[UINib nibWithNibName:@"WYRedPacketListCell" bundle:nil] forCellReuseIdentifier:@"WYRedPacketListCell"];
    self.tableView.rowHeight = 72;
    [self.tableView setSeparatorStyle:(UITableViewCellSeparatorStyleNone)];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WYRedPacketListCell *cell =[tableView dequeueReusableCellWithIdentifier:@"WYRedPacketListCell" forIndexPath:indexPath];
    
    if (!cell) {
        cell = [[NSBundle mainBundle] loadNibNamed:@"WYRedPacketListCell" owner:nil options:nil].firstObject;
    }
    WYMYReceiveList *list =self.dataArray[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell setList:list];
    
    return cell;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    WYRedSectionView *sectionView = [WYRedSectionView getSectionView];
    self.sectionView = sectionView;
    sectionView.delegate = self;
    if (self.receive) {
        [sectionView setReceive:self.receive];
    }
    [sectionView.yearBtn setTitle:[NSString stringWithFormat:@"%@年",self.year] forState:(UIControlStateNormal)];
    return sectionView;
}
-(void)didSelectYear
{
    WEAKSELF
    WYSelectYearView *yearView = [[WYSelectYearView alloc]initWithFrame:CGRectMake(0, 0,kScreenWidth, kScreenHeight)];
    yearView.selectYear = self.year;
   // yearView.backgroundColor = [UIColor clearColor];
    yearView.complte = ^(NSString *year) {
        weakSelf.year = year;
        [weakSelf.sectionView.yearBtn setTitle:[NSString stringWithFormat:@"%@年",year] forState:UIControlStateNormal];
        weakSelf.refresh = YES;
        [weakSelf loadDataWithYear:year];
        
        
    };
    
    
    [yearView show];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
      WYMYReceiveList *list =self.dataArray[indexPath.row];
    if (list.redpacketType.integerValue==0) {
        WYExclusiveController *evc = [[WYExclusiveController alloc]init];
        evc.money = list.receiveAmount;
        [self.navigationController pushViewController:evc animated:YES];
    }else{
        WYNearRedPacket *near = [[WYNearRedPacket alloc]init];
        near.redpacketId = list.redpacketId;
        if ([list.redpacketType isEqualToString:@"1"]) {
            near.redpacketType = @"2";
        }else if([list.redpacketType isEqualToString:@"4"]){
            near.redpacketType = @"3";
        }else{
            near.redpacketType = @"1";
        }
        near.isReceived = @"1";
        WYRedPacketMessageController *rvc = [[WYRedPacketMessageController alloc]init];
        rvc.redPacket = near;
        [self.navigationController pushViewController:rvc animated:YES];
    }
    
    
   
//    [[NSNotificationCenter defaultCenter] postNotificationName:@"seeProduct" object:@{@"service":service}];
   
    
    
    

    
}


-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 224;
}
-(UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:(UITableViewStyleGrouped)];
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
    
    
}
@end
